package com.person.title.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.title.model.Title;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleService;
import com.person.title.service.TitleSubService;

public class TitleSubAction extends BaseAction {

	private TitleSubService titleSubService ;
	private TitleService titleService ;
	private List <TitleSub> titleSubs ;
	private Title title ;
	private TitleSub titleSub ;
	private String titleCode ;
	private String subCode ;
	private String titleCodeGroup ;	
	private String titleName ;	
	private int flag = 0 ;
	
	private String image;
	private String imageFileName;
	/**
	 * 进入添加子栏目界面
	 * 
	 * @return
	 */
	public String preCreate () {
			
		String forward = "" ;
		
		if ((!"".equals(titleSub.getSubId())) && (titleSub.getSubId())!=null) {
			titleSub = this.titleSubService.findTitleSubById(titleSub.getSubId()) ;
			titleCode = titleSub.getTitleCodeGroup() ;
			titleName = titleSub.getSubName() ;			
		} else {
			title = titleService.findSysTitleId(title.getTitleId()) ;
			titleCode = title.getTitleCode() ;
			titleName = title.getTitleName() ;
		} 
		
		if (titleCode.length()<=8) {
			forward = "createSub" ;			
		} else {
			info = getText ("sub.create.fail") ;
			forward = "create_fail" ;
		}
		
		return forward ;
	}
	
		
	/**
	 * 添加子栏目
	 * 
	 */
	public String create () {
		
		String forward = "" ;
		String titleCodeGroup = titleCode + subCode ;
		titleSub.setTitleCodeGroup(titleCodeGroup) ;
		
		char state = this.titleSubService.createTitleSub(titleSub) ;
		if(image!=null){
			titleSub.setSubImage((upload(image,imageFileName)));
		}
		titleSubService.update(titleSub);
		
		
		if(state == '1'){
			info = getText("sub.create.success");	
			flag = 1 ;
			forward = "create_success";
		}else if(state == '2'){
			
			info = getText("sub.code");
			forward = INPUT;
			
		}else if(state == '3'){
			
			info =  getText("sub.name");
			forward = INPUT;
		}else if(state == '4'){
			
			info =  getText("sub.nameEn");
			forward = INPUT;
		}
		
		return forward;
	}
	
	
	/**
	 * 进入修改子栏目节点
	 * 
	 * @return
	 */
	public String preModify () {
		
		titleSub = this.titleSubService.findTitleSubById(titleSub.getSubId()) ; 
		
		titleCodeGroup = titleSub.getTitleCodeGroup() ;
		
		subCode = titleCodeGroup.substring(titleCodeGroup.length()-2, titleCodeGroup.length()) ;
		titleCode = titleCodeGroup.substring(0, titleCodeGroup.length()-2) ;
		
		if (titleCode.length()==2) {
			Title title1 = this.titleService.findTitleByCode(titleCode) ;
			titleName = title1.getTitleName() ;
			
		} else {		
			TitleSub titleSub1 = this.titleSubService.findTitleSubByCode(titleCode) ;
			titleName = titleSub1.getSubName() ;
		}
		return "modifySub" ;
	}
	
	
	/**
	 * 修改子栏目节点
	 * 
	 * @return
	 */
	public String modify () {
		
		String forward = "" ;
		
		String titleCodeGroup1 = titleCode + subCode ;
		titleSub.setTitleCodeGroup(titleCodeGroup1) ;
		
		char state = this.titleSubService.modifyTitleSub(titleSub) ;
		
		if(image!=null&&!image.equals("")){
			titleSub.setSubImage((upload(image,imageFileName)));
		}
		titleSubService.update(titleSub);
		
		char state1 = this.titleSubService.modifyJuniorTitleSub(titleSub, titleCodeGroup) ;
		
		if(state == '1' && state1=='1'){
			info = getText("sub.modify.success");	
			flag = 1 ;
			forward = "modify_success";
		}else if(state == '2'){
			
			info = getText("sub.code");
			forward = INPUT;
			
		}else if(state == '3'){
			
			info =  getText("sub.name");
			forward = INPUT;
		}else if(state == '4'){
			
			info =  getText("sub.nameEn");
			forward = INPUT;
		}
		
		return forward ;
	}
	
	
	
	/**
	 * 删除子栏目节点
	 * 
	 * @return
	 */
	public String delete() {
		String forward = "" ;
		
		if(titleSub != null) {
			titleSub = this.titleSubService.findTitleSubById(titleSub.getSubId()) ;
			char state = this.titleSubService.deleteTitleSub(titleSub) ;
			if(state == '1') {
				info = getText("sub.delete.success") ;
				flag = 1;
				forward = "delete_success" ;
			}else if(state == '0') {
				
				info = getText("sub.delete.fail");
				forward = "delete_success";				
			}
		}
		return forward;
	}
	
	
	
	
	/**
	 * 管理子栏目节点页面
	 * @return
	 */
	public String manageSub() {
		
		titleSub = this.titleSubService.findTitleSubById(titleSub.getSubId()) ;
				
		titleSubs = this.titleSubService.juniorTitleSubList(titleSub.getSubId()) ;
		
		titleCode = titleSub.getTitleCodeGroup() ;
		
		titleName = titleName + "→" + titleSub.getSubName() ;
		
		return "manageSub" ;
	}
	
	
	
public String upload(String image,String imageFileName) {
		
		String imageType = imageFileName.substring(
				imageFileName.lastIndexOf("."));
		String rootDir = request.getRealPath(File.separator)
				+ Constants.TITLE_DIR;
		String picName = String.valueOf(new Date().getTime());
		String imageDir = rootDir + picName + imageType;

		File rootFile = new File(rootDir);
		File imageFile = new File(imageDir);
		if (!rootFile.exists()) {
			rootFile.mkdirs();
		}

		if (imageFile.exists()) {
			imageFile.delete();
		}

		try {
			FileInputStream is = new FileInputStream(image);
			FileOutputStream os = new FileOutputStream(imageDir);
			byte[] buffer = new byte[4096];
			int lenth = 0;
			while ((lenth = is.read(buffer)) > 0) {
				os.write(buffer);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String fileType=picName + imageType;
		return fileType;
	}
	public TitleSubService getTitleSubService() {
		return titleSubService;
	}
	
	public void setTitleSubService(TitleSubService titleSubService) {
		this.titleSubService = titleSubService;
	}
	
	public List<TitleSub> getTitleSubs() {
		return titleSubs;
	}
	
	public void setTitleSubs(List<TitleSub> titleSubs) {
		this.titleSubs = titleSubs;
	}
	
	public TitleSub getTitleSub() {
		return titleSub;
	}
	
	public void setTitleSub(TitleSub titleSub) {
		this.titleSub = titleSub;
	}
	
	public int getFlag() {
		return flag;
	}
	
	public void setFlag(int flag) {
		this.flag = flag;
	}
	
	public TitleService getTitleService() {
		return titleService;
	}
	
	public void setTitleService(TitleService titleService) {
		this.titleService = titleService;
	}

	public Title getTitle() {
		return title;
	}

	public void setTitle(Title title) {
		this.title = title;
	}

	public String getTitleCode() {
		return titleCode;
	}

	public void setTitleCode(String titleCode) {
		this.titleCode = titleCode;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}


	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}


	public String getTitleCodeGroup() {
		return titleCodeGroup;
	}


	public void setTitleCodeGroup(String titleCodeGroup) {
		this.titleCodeGroup = titleCodeGroup;
	}


	public String getImage() {
		return image;
	}


	public void setImage(String image) {
		this.image = image;
	}


	public String getImageFileName() {
		return imageFileName;
	}


	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	
}
